const Koa = require('koa')
  , router = require('koa-router')()
  , json = require('koa-json')
  , logger = require('koa-logger'); 
  const bodypar=require('koa-bodyparser');// 引入各种依赖
const app=new Koa();
const jwt=require('koa-jwt');
const path=require('path');
const server=require('koa-static');
app.use(bodypar());
const auth=require('./server/routes/auth.js');
const api=require('./server/routes/api.js');
app.use(json());
app.use(logger());


app.use(async function(ctx,next){
	let start=new Date().getTime();
	await next();
	let ms=new Date().getTime()-start;
	console.log('%s %s - %s', ctx.method, ctx.url, ms)
})

app.on('error', function(err, ctx){
  console.log('server error', err);
});
router.use('/auth',auth.routes());
router.use("/api",jwt({secret: 'vue-koa-demo'}),api.routes())
app.use(router.routes(),bodypar());
app.use(server(path.resolve('dist')));
app.listen(8889,() => {
  console.log('Koa is listening in 8889');
});

module.exports = app;